package com.erp.framework.controller.form;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.SimpleConstants;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class SimpleSearchStyleForm extends SimpleCoreForm {
    protected String idx[];
    protected String isDelete[];
    protected int sidx = 0;
    protected String scrollTo;

    protected ArrayList resultList = new ArrayList();

    public SimpleBaseVo getBizModel(int i) {
        return (SimpleBaseVo) resultList.get(i);
    }

    public SimpleSearchStyleForm() {
    }

    public String getEditType() {
        return "";
    }

    public String[] getIdx() {
        return this.idx;
    }

    public String[] getIsDelete() {
        return this.isDelete;
    }

    public int getSidx() {
        return sidx;
    }

    public ArrayList getResultList() {
        return resultList;
    }

    public SimpleBasePk getSelected() {
        return null;
    }

    public void setIdx(String[] idx) {
        this.idx = idx;
    }

    public String getScrollTo() {
        return scrollTo;
    }

    public void setScrollTo(String scrollTo) {
        this.scrollTo = scrollTo;
    }

    public void setIsDelete(String[] isDelete) {
        this.isDelete = isDelete;
    }

    public void setSidx(int sidx) {
        this.sidx = sidx;
    }

    public void setResultList(ArrayList resultList) {
        this.resultList = resultList;
        if (this.resultList != null && this.resultList.size() > 0 && this.getSearchResultValueProperties() != null && !this.getSearchResultValueProperties().isEmpty()) this.buildResultListValues();
    }

    public void buildResultListValues() {
    }

    public int getTotalPage() {
        int tPage = (int) (resultList.size() / SimpleConstants.MAX_DISPLAY);
        if (resultList.size() % SimpleConstants.MAX_DISPLAY != 0)
            tPage += 1;
        return tPage;
    }

    public int getCurrentPage() {
        int cPage = (int) ((sidx + 1) / SimpleConstants.MAX_DISPLAY);
        if ((sidx + 1) % SimpleConstants.MAX_DISPLAY != 0)
            cPage += 1;
        return cPage;
    }

    public boolean isFirstPage() {
        int page = getCurrentPage();

        return page == 1;
    }

    public boolean isLastPage() {
        int page = getCurrentPage();

        return page == getTotalPage();
    }

    public String getLastPageSidx() {
        return String.valueOf((getTotalPage() - 1) * SimpleConstants.MAX_DISPLAY);
    }

    public void otherReset(ActionMapping mapping, HttpServletRequest request) {
        idx = null;
        isDelete = null;
    }

    public void resetForm() {
        resetSearchCriteria();
        clearResultList();
    }

    // reset the searching criteria (field that starts with "srch")
    public void resetSearchCriteria() {
        try {
            BeanUtil.setSpecificStringPropertiesEmpty(this, "srch");
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    // clear the result list
    public void clearResultList() {
        resultList.clear();
        sidx = 0;
        this.searchResultValuesMap = null;
        this.clearResultListValues();
    }

    public void clearResultListValues() {
    }

    public void otherValidate(ActionErrors errors) {
    }

    public Set getSearchResultValueProperties() {
        return Collections.EMPTY_SET;
    }

    private Map searchResultValuesMap;

    public Map getSearchResultValuesMap() {
        if (this.getSearchResultValueProperties().isEmpty()) {
            this.searchResultValuesMap = new HashMap();
        } else if (searchResultValuesMap == null) {
            Map valuesMap = new HashMap();
            for (Iterator results = CommonUtil.getIterator(this.getResultList()); results.hasNext();) {
                Object result = results.next();
                for (Iterator properties = CommonUtil.getIterator(this.getSearchResultValueProperties()); properties.hasNext();) {
                    String property = CommonUtil.nullToString(properties.next());
                    Set keys = null;
                    if (valuesMap.containsKey(property)) {
                        keys = (Set) valuesMap.get(property);
                    } else {
                        keys = new HashSet();
                        valuesMap.put(property, keys);
                    }
                    try {
                        Object key = BeanUtil.getPropertyDefault(result, property);
                        if (!CommonUtil.isEmpty(key)) keys.add(key);
                    } catch (Exception e) {
                        Logger.debug(this, e.getMessage());
                        continue;
                    }
                }
            }
            searchResultValuesMap = valuesMap;
        }
        return searchResultValuesMap;
    }

}
